package com.xywl.escort.view.coupon

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.text.Spannable
import android.text.SpannableString
import android.text.TextUtils
import android.text.style.AbsoluteSizeSpan
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.trello.rxlifecycle2.LifecycleTransformer
import com.xywl.escort.R
import com.xywl.escort.base.BaseActivity
import com.xywl.escort.bean.CouponBean
import com.xywl.escort.utils.ToastUtil
import dagger.android.AndroidInjection
import kotlinx.android.synthetic.main.activity_coupon_detail.*
import timber.log.Timber
import javax.inject.Inject

@Route(path = "/xy/coupon/detail")
class CouponDetailActivity : BaseActivity(), CouponDetailContract.View {
    @Autowired
    @JvmField
    var couponId = ""

    // 0 锦囊红包列表页面跳转进入 1 显示立即使用按钮页面(提交订单、认购付款弹窗页面) 2 不显示立即使用按钮(订单详情进入)
    @Autowired
    @JvmField
    var type = 0

    private var coupon: CouponBean? = null

    @Inject
    lateinit var mPresenter: CouponDetailContract.Presenter

    override fun onCreate(savedInstanceState: Bundle?) {
        AndroidInjection.inject(this)
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_coupon_detail)
        ARouter.getInstance().inject(this)
        showBackButton()
        setTitleText(R.string.tips_coupon)

        mPresenter.takeView(this)
        mPresenter.redPacketDetail(couponId)

        btn_use.setOnClickListener {
            val intent = Intent()
            intent.putExtra("coupon", coupon)
            intent.putExtra("type", type)
            setResult(Activity.RESULT_OK, intent)
            finish()
        }
    }

    override fun isActive(): Boolean {
        return false
    }

    override fun <T : Any?> bindToLife(): LifecycleTransformer<T> {
        return this.bindToLifecycle()
    }

    override fun redPacketDetailSuccess(dataBean: CouponBean?) {
        coupon = dataBean
        Timber.e("coupon=$coupon")
        val couponName = "红包名称 : " + coupon!!.name
        text_coupon_name.text = couponName
        val amountOfMoney = coupon!!.amountOfMoneyYuan.toInt()
        val couponQuota = "奖励额度 : " + amountOfMoney + "元"
        text_coupon_quota.text = couponQuota
        val couponPrice = "￥$amountOfMoney"
        val spanString = SpannableString(couponPrice)
        val span = AbsoluteSizeSpan(12, true)
        spanString.setSpan(span, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        text_price.text = spanString

        val condMinPriceYuan = "满" + coupon!!.condMinPriceYuan + "元可用"
        text_assets.text = condMinPriceYuan

        val useScene = coupon!!.useScene
        if (!TextUtils.isEmpty(useScene)) {
            var text = "适用场景 : "
            var couponUseCondition = ""
            when (useScene) {
                "1" -> {
                    text += "预售项目 "
                    couponUseCondition = "使用条件 : 投资" + coupon!!.condMinDay + "天及以上期限产品，单笔满" + coupon!!.condMinPriceYuan + "元可用"
                }
                "2" -> {
                    text += "直采商品 "
                    couponUseCondition = "使用条件 : 单笔满" + coupon!!.condMinPriceYuan + "元可用"
                }
                "3" -> {
                    text += "转卖商品 "
                }
                else -> {
                }
            }
            text_coupon_scenes.text = text
            text_coupon_use_condition.text = couponUseCondition
        }
        if (coupon!!.valid == "-1") {
            val couponTime = "有效时长 : 永久有效"
            text_coupon_effective_time.text = couponTime
        } else {
            val couponTime = "有效期至 : " + coupon!!.validTime
            text_coupon_effective_time.text = couponTime
        }
        val bizCode = coupon!!.bizCode
        if (bizCode == 1) {
            text_coupon_condition.visibility = View.VISIBLE
            val couponCondition = "获得条件 : 用户注册"
            text_coupon_condition.text = couponCondition
        } else {
            text_coupon_condition.visibility = View.GONE
        }

        val useStatus = coupon!!.useStatus
        when (type) {
            0 -> {
                btn_use.visibility = View.VISIBLE
            }
            1 -> {
                btn_use.visibility = View.VISIBLE
            }
            2 -> {
                btn_use.visibility = View.GONE
            }
        }

        when (useStatus) {
            1 -> {
                btn_use.text = "立即使用"
                btn_use.isEnabled = true
            }
            2 -> {
                btn_use.text = "已使用"
                btn_use.isEnabled = false
            }
            3 -> {
                btn_use.text = "已过期"
                btn_use.isEnabled = false
            }
        }
    }

    override fun redPacketDetailFailure(msg: String?) {
        btn_use.visibility = View.GONE
        ToastUtil.showTip(this, msg)
    }
}
